package com.langxin.site.model;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.langxin.site.model.vo.BaseVo;

/**
 * 分页对象
 * 
 * @author Ian
 * @date 2014-12-7
 */
public class PageInfo<T extends Object> implements Serializable {
	Logger log = LoggerFactory.getLogger(PageInfo.class);

	private static final long serialVersionUID = 1L;
	// -- 公共变量 --//
	public static final String ASC = "asc";
	public static final String DESC = "desc";

	// -- 分页参数 --//
	protected int currentPageNo = 1;
	protected int pageSize = 20;
	protected int page = 1;
	protected int start = 0; // 起始索引
	protected int end = 20; // 结束索引
	protected String orderBy = null;
	protected String order = null;

	// -- 返回结果 --//
	protected List<T> records = Lists.newArrayList();
	// -- 总页数-- //
	protected int totalPage;
	// -- 总记录数
	private int totalCount = -1;

	// -- 构造函数 --//
	public PageInfo() {
	}

	public PageInfo(int pageSize) {
		this.pageSize = pageSize;
	}

	@Override
	public Object clone() {
		Object obj = null;
		try {
			obj = super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	/**
	 * 开始索引
	 * 
	 * @return
	 */
	public int getStart() {
		return (page - 1) * pageSize;
	}

	/**
	 * 结束索引
	 * 
	 * @return
	 */
	public int getEnd() {
		return page * pageSize;
	}

	/**
	 * 总页数
	 * 
	 * @return
	 */
	public int getTotalPage() {
		if (this.totalCount == -1) {
			return 0;
		}
		return this.totalCount % this.pageSize > 0 ? this.totalCount / this.pageSize + 1
				: this.totalCount / this.pageSize;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	/**
	 * 上一页
	 * 
	 * @return
	 */
	public int getPrePage() {
		return this.page - 1 < 1 ? 1 : this.page - 1;
	}

	/**
	 * 下一页
	 * 
	 * @return
	 */
	public int getNextPage() {
		return this.page + 1 > this.getTotalPage() ? this.getTotalPage()
				: this.page + 1;
	}


	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List<T> getRecords() {
		return records;
	}

	public void setRecords(List<T> records) {
		this.records = records;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getCurrentPageNo() {
		return currentPageNo;
	}

	public void setCurrentPageNo(int currentPageNo) {
		this.currentPageNo = currentPageNo;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	
	@SuppressWarnings("hiding")
	public <T extends BaseVo> void fill(T t){
		this.setPage(t.getPage());
		this.setPageSize(t.getPageSize());
	}
}
